package org.example.config;

/**
 * @Description: 持久层的配置
 * @Author:曹震
 * @Date: 2024/11/19 16:02
 */

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

//@PropertySource("classpath:jdbc.properties")
@Configuration
@MapperScan("org.example.mapper")
public class MapperConfig {
//将SqlSessionFactoryBean放入到ioc容器当中
@Bean
   public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DruidDataSource dataSource){
       //确定连接池
       SqlSessionFactoryBean sqlSessionFactory =new  SqlSessionFactoryBean();
       sqlSessionFactory.setDataSource(dataSource);
       //配置文件 setting
       org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
       //设置驼峰命名法的设置
       configuration.setMapUnderscoreToCamelCase(true);
       //设置日志设置
       configuration.setLogImpl(Slf4jImpl.class);
       //设置映射关系，这样在多表的时候就不要写result标签了
       configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
       sqlSessionFactory.setConfiguration(configuration);
       sqlSessionFactory.setTypeAliasesPackage("org.example.pojo");
        //设置插件，创建对象，然后在给值
       PageInterceptor pageInterceptor = new PageInterceptor();
       //属性对象
       Properties properties = new Properties();
       //设置属性值
       properties.setProperty("helperDialect", "mysql");
       //装配
       pageInterceptor.setProperties(properties);
       //装配
       sqlSessionFactory.addPlugins(pageInterceptor);
       return sqlSessionFactory;
   }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        //Mapper代理对象的FactorBean-》指定一个包-》mapper接口——》sqlsessionFactory-》sqlSessionFactory
        //getMapper -》得到代理对象，加入到ioc容器当中
        MapperScannerConfigurer mapperScanner = new MapperScannerConfigurer();
        mapperScanner.setBasePackage("org.example.mapper");
        return  mapperScanner;

    }

}
